﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkflowWizart.Lib.Utility.Costants;

namespace WorkflowWizart.Lib.Entities
{
    public class ListData
    {
        public string Name
        {
            get
            {
                return DisplayName.Trim().Replace(" ", "_");
            }
        }

        public string DisplayName { get; set; }

        private List<ListDataColumn> _Columns = null;
        public List<ListDataColumn> Columns
        {
            get
            {
                return _Columns;
            }
        }

        private List<ListDataRow> _Rows = null;
        public List<ListDataRow> Rows
        {
            get
            {
                return _Rows;
            }
        }
         
        public ListDataRow this[int index]
        {
            get { return Rows[index]; }
            set { Rows[index] = value; }
        }

        public ListDataRow this[string index]
        {
            get { return Rows.SingleOrDefault(r => r.Id == index); }
            set
            {
                var row = Rows.SingleOrDefault(r => r.Id == index);
                row = value;
            }
        }

        public ListDataColumn AddColumn(ColumnDataTypes dataType, string name, string relationalDataColumnId)
        {
            ListDataColumn column = new ListDataColumn();
            column.DataType = dataType;
            column.Name = name;
            column.ParentList = this;
            column.RelationalDataColumnId = relationalDataColumnId;
            return AddColumn(column);
        }
        public ListDataColumn AddColumn(ListDataColumn column)
        {
            if (_Columns == null)
            {
                _Columns = new List<ListDataColumn>();
            }
            column.ParentList = this;
            Columns.Add(column);
            return column;
        }
        public ListDataRow AddRow(List<object> data)
        {
            return AddRow(string.Empty, data);
        }
        public ListDataRow AddRow(string id, List<object> data)
        {
            ListDataRow row = new ListDataRow();
            row.CreateDate = DateTime.Now;
            row.Id = id;
            row.Status = 1;
            row.RowData = data;
            return AddRow(row);
        }
        public ListDataRow AddRow(ListDataRow row)
        {
            if (_Rows == null)
            {
                _Rows = new List<ListDataRow>();
            }
            if (string.IsNullOrEmpty(row.Id))
            {
                row.Id = Guid.NewGuid().ToString();
            }
            row.ParentList = this;
            Rows.Add(row);
            return row;
        }
    }
}
